# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Contributor: David Macek <david.macek.0@gmail.com>
# Contributor: Mateusz Mikuła <mati865@gmail.com>
# Contributor: Alethea Rose <alethea@alethearose.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=nodejs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=23.5.0
pkgrel=3
pkgdesc="An open-source, cross-platform JavaScript runtime environment (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://nodejs.org/"
msys2_repository_url='https://github.com/nodejs/node'
msys2_documentation_url='https://nodejs.org/download/docs'
msys2_references=(
  'archlinux: nodejs'
  "cpe: cpe:/a:nodejs:node.js"
  "cpe: cpe:/a:nodejs:nodejs"
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "winpty")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-winpthreads")
source=("https://nodejs.org/dist/v${pkgver}/node-v${pkgver}.tar.xz"
        0002-Fix-system-icu-build.patch
        0004-Define-localtime_s-for-MinGW.patch
        0005-Remove-.lib-suffix-on-linked-libraries.patch
        0006-Define-_WIN32_WINNT-in-node.gypi.patch
        0007-Skip-sys-resource-and-unneeded-includes.patch
        0008-Link-with-municode-and-extern-wmain-for-Unicode.patch
        0010-Fix-incorrect-test-assumptions-for-MinGW.patch
        0011-Properly-detect-architecture.patch
        0012-Use-shell-wrapper-script-for-npm-on-MSYS2-MinGW-w64.patch
        0013-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch
        0015-Remove-v8-export-private-refs.patch
        0016-Do-not-rely-on-__STDC_FORMAT_MACROS.patch
        0017-Do-not-get-confused-by-the-presence-of-winpthread.patch
        0021-Use-wide-character-Win32-API-functions-to-handle-wch.patch
        0022-Disable-MSVC_HACK.patch
        0023-Make-sure-that-__rdtsc-is-declared.patch
        0024-Fix-gyp-mingw.patch
        0025-Fix-globalPrefix-on-mingw.patch
        0026-Embed-manifest-in-node-exe.patch
        0101-node-buildflags-fixes.patch
        0102-set-cxx-standard-to-gnu.patch
        0103-node-gyp-support-mingw-toolchain.patch
        0104-v8-fix-static-assert-implementations.patch
        0105-v8-fix-conflicting-functions.patch
        0106-v8-support-clang-in-mingw-mode.patch
        0107-v8-disable-system-instrumentation.patch
        0108-v8-disable-etw-stack-walking.patch
        0109-v8-builtin-deps-fixes.patch
        0110-v8-heap-use-proper-sources.patch
        0201-v8-buildflags-fixes.patch
        0202-v8-builtin-deps-buildflags-fixes.patch
        node)
sha256sums=('32e77b36c0774c68baab41bc7c2acc58663ca0a2b7c4d3e9bec6f761c15fdac0'
            'cb56e4f4160dae1041448be64760296634a3aa86fb163ccbfd645f4ae96d2026'
            'e9a8bd133ec58cdf1987ec4c3dd7f0b2e290ebd708d0f988f56e28c01f376767'
            'c420caaa03bc1c809f48ddcb19157ba0284e33fb6cffff8354c9fc4aa75e2afc'
            '38f7e93770f3788e7d23915c8fe2e3e625f2191f827a389115930c9a2313aec8'
            '5cb60fcd16378926fa89ffd114b8244bb063bc8a890f141858952084a9aabd0d'
            'b04f402e65556bcda04f021c7f159ed85238c3517b40cdf38aaba53169754666'
            '8a685608e4ef8d2a53222135dfbc51bb337b043939dcd7f50683acd8787f4fe1'
            'c2daca3eec876a98adcbb35c7438afe24beb8a1953b62947912f7ea25c63fcb9'
            '63cc0a5996fcbdc0959ede2755740493b7c3eda5d878dacc76f6ddec22321c65'
            '442ffeefa393487940735247b5cad9eb585d9b3667a45e5efb7a4973daac9261'
            '709f14cc5abf36bad8b24d014985a0a77718739e0c1dc932565796a1480d1887'
            '99e64bacf33db5cee211eba00841368d22b40030a91e0475c93c4fe4f76f27d1'
            '67953237885a5b990f326b7155780ea082d933ee40d071cba22953d9d8299d3d'
            '10944ecd5dbc60c8d698ecb2b4ed5acb0289a40c5376fe0aea80c409206d7f8a'
            '208e9816d851245271a78516e86505c03f3fc7918663372974a128e2f1f09c62'
            'c7d56e545bf21328b0e9f88add15dc7bc39f3adb90d2079bf3448cc8746fa9e3'
            '930d964034563ba9ffc49c65dfd1e00b19d8906447b9e5186543acfe531cc4c2'
            '701cef4edacfd6b2512bb7a42ef388eb704bc500108062af4ead0439667da416'
            '7d498f8090ab05458f28a5d2c60ef8511d2b48bc5368fda06922c35ca2584a22'
            'edc0fbac6d8d32d182de397197844ac0ce9e4f15bc5ba316e9406147d150c539'
            '18ca0c99da6761aeba3b2e8b6fa23787d7d3114e1798fecd9037e9a7a8bc93c1'
            'd4343762547064931ccfacca4906ad042f4ef81f73f86ee654ee152e0bc8844f'
            '67d40fde6aaa108db2de4d946874857607bade8fa0ee76d77b759439fa80f246'
            '492758c11d287d88ea035d84f5ab3eb0dc76da0b8552f6ad13c4cd89597f4183'
            'd5cc3e2bf8b506dce2a96105eb6a6ff300088a7b488cbe6d390efdb7d6c630e5'
            'b07a5125182104cd43a1bd07a637096962fc475664e2292cc4bd79a69ca442da'
            '22330afcfd297e680062f89d62816afa7b3e6b316950927fe31c6eff31642f26'
            '892d5f1d47079a5389be8078f87cb43a68e73b7809e9e2f58b30e57e06506e57'
            '88aec176748473f6aa81344a9f9f4752d1d366fa5b0a9be1ce42d41f80173565'
            'a884c89fb7838274f5b4c845f53e93aaadc9fe105801726c0c86fd185d97ba20'
            '39e1ddc31b445672a657760a2d494c2c143b9f0b0c5106d468ffb8d478e438ec'
            '284251ff443506cd61530a026afb421d0084e4ad25c037bcfe3a045246d729a2')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "node-v${pkgver}"

  apply_patch_with_msg \
    0002-Fix-system-icu-build.patch \
    0004-Define-localtime_s-for-MinGW.patch \
    0005-Remove-.lib-suffix-on-linked-libraries.patch \
    0006-Define-_WIN32_WINNT-in-node.gypi.patch \
    0007-Skip-sys-resource-and-unneeded-includes.patch \
    0008-Link-with-municode-and-extern-wmain-for-Unicode.patch \
    0010-Fix-incorrect-test-assumptions-for-MinGW.patch \
    0011-Properly-detect-architecture.patch \
    0012-Use-shell-wrapper-script-for-npm-on-MSYS2-MinGW-w64.patch \
    0013-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch \
    0015-Remove-v8-export-private-refs.patch \
    0016-Do-not-rely-on-__STDC_FORMAT_MACROS.patch \
    0017-Do-not-get-confused-by-the-presence-of-winpthread.patch \
    0021-Use-wide-character-Win32-API-functions-to-handle-wch.patch \
    0022-Disable-MSVC_HACK.patch \
    0023-Make-sure-that-__rdtsc-is-declared.patch \
    0024-Fix-gyp-mingw.patch \
    0025-Fix-globalPrefix-on-mingw.patch \
    0026-Embed-manifest-in-node-exe.patch

  apply_patch_with_msg \
    0101-node-buildflags-fixes.patch \
    0102-set-cxx-standard-to-gnu.patch \
    0103-node-gyp-support-mingw-toolchain.patch \
    0104-v8-fix-static-assert-implementations.patch \
    0105-v8-fix-conflicting-functions.patch \
    0106-v8-support-clang-in-mingw-mode.patch \
    0107-v8-disable-system-instrumentation.patch \
    0108-v8-disable-etw-stack-walking.patch \
    0109-v8-builtin-deps-fixes.patch \
    0110-v8-heap-use-proper-sources.patch

  apply_patch_with_msg \
    0201-v8-buildflags-fixes.patch \
    0202-v8-builtin-deps-buildflags-fixes.patch

  find 'deps/npm/node_modules' -depth -name '*.orig' -exec rm -f {} ';'
}

build() {
  cd "node-v${pkgver}"

  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=( --debug )
  fi

  local opt_cpu="x64"
  if [[ ${CARCH} == aarch64 ]]; then
    opt_cpu="arm64"
  fi

  if [[ ${MSYSTEM} == CLANG* ]]; then
    extra_config+=( --clang-cl=1 )
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ./configure \
    --prefix="${MINGW_PREFIX}" \
    --dest-os=win \
    --dest-cpu=${opt_cpu} \
    --shared \
    --shared-cares \
    --shared-openssl \
    --shared-zlib \
    --with-intl=system-icu \
    "${extra_config[@]}"

  make

  # flatten the nested node_modules/ hierarchy
  test -z "$(find deps/npm/node_modules | grep '^.\{140,\}$')" ||
  (cd deps/npm &&
   cp -R . ../npm.bup &&
   rm -rf node_modules &&
   ../../out/Release/node.exe ../npm.bup/bin/npm-cli.js install --production)
}

check() {
  cd "node-v${pkgver}"

  # A few tests still fail due to a bug with pausing/resuming file streams
  ${MINGW_PREFIX}/bin/python tools/test.py --mode=release -J \
    cctest doctool inspector message parallel sequential tick-processor || true
}

package() {
  cd "node-v${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  python tools/install.py install --dest-dir="${pkgdir}" --prefix="${MINGW_PREFIX}"

  mv -v "${pkgdir}${MINGW_PREFIX}/bin/node"{.exe,_exe}
  install -Dm755 "${srcdir}/node" "${pkgdir}${MINGW_PREFIX}/bin/node"
  mv -v "${pkgdir}${MINGW_PREFIX}/bin/node"{_exe,.exe}

  # install the cmd-based wrappers
  cp -f "${srcdir}"/node-v${pkgver}/deps/npm/bin/node-gyp-bin/node-gyp.cmd "${pkgdir}${MINGW_PREFIX}/bin"
  cp -f "${srcdir}"/node-v${pkgver}/deps/npm/bin/{corepack,npm,npx}.cmd "${pkgdir}${MINGW_PREFIX}/bin"

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/nodejs"
  cp -r doc/api/{*.html,assets} "${pkgdir}${MINGW_PREFIX}/share/doc/nodejs"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/nodejs/LICENSE"
}
